﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerMauSuatAnChiTietDao : IMauSuatAnChiTietDao
    {
        public SqlServerMauSuatAnChiTietDao()
        {
            SQLUtility.TableName = "MauSuatAnChiTiet";
        }

        //Trả về đối tượng báo ăn
        private MauSuatAnChiTiet MakeMauSuatAnChiTiet(DataRow row)
        {

            Guid _id = Guid.Parse(row["Id"].ToString());
            Guid _mauSuatAnId = Guid.Parse(row["MauSuatAnId"].ToString());
            Guid _thucPhamId = Guid.Parse(row["ThucPhamId"].ToString());
            int _soLuong = int.Parse(row["SoLuong"].ToString());

         return new MauSuatAnChiTiet(_id, _mauSuatAnId, _thucPhamId, _soLuong);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<MauSuatAnChiTiet> MakeMauSuatAnChiTiets(DataTable dt)  
        {
            IList<MauSuatAnChiTiet> list = new List<MauSuatAnChiTiet>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeMauSuatAnChiTiet(row));
            }
            return list;
        }
        private DbParameter[] Take(MauSuatAnChiTiet MauSuatAnChiTiet)
        {
            DbParameter[] parameters = new DbParameter[4];
            parameters[0] = new SqlParameter("@Id", MauSuatAnChiTiet.Id);
            parameters[1] = new SqlParameter("@MauSuatAnId", MauSuatAnChiTiet.MauSuatAnId);
            parameters[2] = new SqlParameter("@ThucPhamId", MauSuatAnChiTiet.ThucPhamId);
            parameters[3] = new SqlParameter("@SoLuong", MauSuatAnChiTiet.SoLuong);
          
            return parameters;
        }

        public IList<Business_Objects.MauSuatAnChiTiet> GetMauSuatAnChiTiet()
        {
            SQLUtility.TableName = "MauSuatAnChiTiet";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeMauSuatAnChiTiets(dt); 
        }

        public Business_Objects.MauSuatAnChiTiet GetMauSuatAnChiTietByID(string MauSuatAnChiTietID)
        {
            SQLUtility.TableName = "MauSuatAnChiTiet";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@Id", MauSuatAnChiTietID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeMauSuatAnChiTiet(row);
            }
        }

        public void InsertMauSuatAnChiTiet(Business_Objects.MauSuatAnChiTiet MauSuatAnChiTiet)
        {
            SQLUtility.TableName = "MauSuatAnChiTiet";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(MauSuatAnChiTiet), true);
        }

        public int UpdateMauSuatAnChiTiet(Business_Objects.MauSuatAnChiTiet MauSuatAnChiTiet)
        {
            SQLUtility.TableName = "MauSuatAnChiTiet";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(MauSuatAnChiTiet)); }
            catch { return -1; }
        }

        public int DeleteMauSuatAnChiTiet(Business_Objects.MauSuatAnChiTiet MauSuatAnChiTiet)
        {
            SQLUtility.TableName = "MauSuatAnChiTiet";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(MauSuatAnChiTiet)); }
            catch { return -1; }
        }
    }
}
